cd "C:\Users\Ricardo Bello-Gomez\Documents\Virtual\Experiment"
use maindata, clear

//Balancing tables
gen educ = .
replace educ = 11 if Nivel=="b" | Nivel=="B"
replace educ = 13 if Nivel=="c" | Nivel=="d"
replace educ = 16 if Nivel=="e" | Nivel=="E"
replace educ = 18 if Nivel=="f" | Nivel=="F"
replace educ = 19 if Nivel=="g"

gen highschool = 0
replace highschool = 1 if educ == 11
gen somecoll = 0
replace somecoll = 1 if educ == 13
gen college = 0
replace college = 1 if educ == 16
gen postgrad = 0
replace postgrad = 1 if educ > 16

gen female = 0
replace female = 1 if Sexo=="Mujer"

rename Edad age
rename revenue* revenue
gen revbill = revenue/1000
gen pop = pobl/1000

gen mayor_l = (codpartido_1==1)
gen mayor_c = (codpartido_1==2)
gen mayor_cr = (codpartido_1==165)
gen mayor_u = (codpartido_1==198)
gen mayor_cd = (codpartido_1==644)
gen mayor_v = (codpartido_1==645)
gen mayor_oc = (codpartido_1==652)
gen mayor_asi = (codpartido_1==654)

gen revpc = revenue/(pop*1000)

gen border = 0
replace border = 1 if Departamento=="La Guajira"
replace border = 1 if Departamento=="Cesar"
replace border = 1 if Departamento=="Norte de Santander"
replace border = 1 if Departamento=="Arauca"
replace border = 1 if Departamento=="Vichada"
replace border = 1 if Departamento=="Guainía"

replace Explocal = 0 if Explocal==.
replace ExpDepartamental = 0 if ExpDepartamental==.
replace ExpNacional = 0 if ExpNacional==.
gen pubexp = Explocal + ExpDepartamental + ExpNacional

gen indrur = rural*100/(rural+urban)

scalar aux=1
matrix balance_edu = J(25,4,.)
matrix balance_rel = J(25,4,.)
foreach x in educ female age pubexp revbill pop pmigrant revpc border indrur discapital disbogota sgover nbi margin former mayor_l mayor_c mayor_cr mayor_u mayor_cd mayor_v mayor_oc mayor_asi {
	ttest `x' if S2==1, by(educpol)
	matrix balance_edu[aux,1] = r(mu_1)
	matrix balance_edu[aux,2] = r(mu_2)
	matrix balance_edu[aux,3] = r(t)
	matrix balance_edu[aux,4] = r(p)

	ttest `x' if S2==1, by(relatives)
	matrix balance_rel[aux,1] = r(mu_1)
	matrix balance_rel[aux,2] = r(mu_2)
	matrix balance_rel[aux,3] = r(t)
	matrix balance_rel[aux,4] = r(p)
	
	scalar aux = aux + 1
}

matlist balance_edu
matlist balance_rel

//Labels
label variable educ "Mayor's education (years)"
label variable female "Female mayor = 1" 
label variable age "Mayor's age (years)"
label variable revenue "Municipal revenue (illions of COP)"
label variable pop "Municipal population (thousands)" 
label variable pmigrant "Migrants (% of total population)"
label variable revpc "Municipal revenue per capita (Millions of COP)"
label variable border "Departamento bordering Venezuela"
label variable discapital "Municipality's lineal distance to departamento capital"
label variable disbogota "Municipality's lineal distance to Bogota"
label variable sgover "Vote share for gubernatorial winner"
label variable mayor_l "Mayor from Liberal Party" 
label variable mayor_c "Mayor from Conservative Party"
label variable mayor_cr "Mayor from Radical Change Party"
label variable mayor_u "Mayor from Social Unity Party"
label variable mayor_cd "Mayor from Democratic Center Party"
label variable mayor_v "Mayor from Green Alliance Party"
label variable mayor_oc "Mayor from Citizen Option Party"
label variable mayor_asi "Mayor from Social Indpt Alliance"
label variable margin "Mayor's margin of victory (%)"
label variable pubexp "Mayor's public experience (yrs)"
label variable indrur "Share of rural population"
label variable nbi "Unsatisfied basic needs (% of households)"
label variable formermayor "Previous terms as mayor"

label variable educpol "Project type (1 = Classrooms)"
label variable relatives "Migrants have local relatives (1 = Yes)"

label variable IIIChoice "Project formulation"
label variable IVChoice "Project execution"
label variable formul "Project formulation"
label variable execut "Project execution"

	//TABLA PRINCIPAL - MLOGIT
eststo m1: mlogit formul educpol relatives
eststo m2: mlogit execut educpol relatives
esttab m1 m2 using multi_exp.rtf, replace unstack noomitted compress se label star(* 0.1 ** 0.05 *** 0.01)

eststo m3: mlogit execut educpol relatives educ female age pubexp revbill pop pmigrant revpc border indrur discapital disbogota sgover nbi margin former mayor*
esttab m3 using multi_cov.rtf, replace unstack noomitted compress se label star(* 0.1 ** 0.05 *** 0.01)

	//OLS national results
eststo n1: reg formu_nal educpol relatives
eststo n2: reg exec_nal educpol relatives
eststo n3: reg all_nal educpol relatives
esttab n1 n2 n3 using nat_exp.rtf, replace compress se label star(* 0.1 ** 0.05 *** 0.01)

eststo n1: reg formu_nal educpol relatives educ female age pubexp revbill pop pmigrant revpc border indrur discapital disbogota sgover nbi margin former mayor*
eststo n2: reg exec_nal educpol relatives educ female age pubexp revbill pop pmigrant revpc border indrur discapital disbogota sgover nbi margin former mayor*
eststo n3: reg all_nal educpol relatives educ female age pubexp revbill pop pmigrant revpc border indrur discapital disbogota sgover nbi margin former mayor*
esttab n1 n2 n3 using nat_cov.rtf, replace compress se label star(* 0.1 ** 0.05 *** 0.01)

//Manipulation checks
eststo m1: mlogit formul educpol relatives if R1==1
eststo m2: mlogit execut educpol relatives if R1==1
esttab m1 m2 using multi_exp_check.rtf, replace unstack noomitted compress se label star(* 0.1 ** 0.05 *** 0.01)

eststo n1: reg formu_nal educpol relatives  if R1==1
eststo n2: reg exec_nal educpol relatives  if R1==1
eststo n3: reg all_nal educpol relatives  if R1==1
esttab n1 n2 n3 using nat_exp_check.rtf, replace compress se label star(* 0.1 ** 0.05 *** 0.01)
